<?php

/**
 * Class for render view card * 
 * 
 * @author Tuan Nguyen <nganhtuan63@gmail.com>
 * @version 1.0
 * @package common.blocks.view_card */
class ViewCardBlock extends CWidget {

    //Do not delete these attr block, page and errors
    public $id = 'view_card';
    public $block = null;
    public $errors = array();
    public $page = null;
    public $layout_asset = '';

    public function setParams($params) {
        return;
    }

    public function run() {
        $this->renderContent();
    }

    protected function renderContent() {
        if (isset($this->block) && ($this->block != null)) {
            //Start working with view card here
            $params = b64_unserialize($this->block['params']);
            $this->setParams($params);
            if (isset($_POST['submitcard'])) {

                if ($_POST['submitcard']) {
                    $item = $_POST['items'];
                    if (Yii::app()->shoppingCart->getItemsCount() > 0) {
                        foreach (Yii::app()->shoppingCart as $key => $position) {

                            if (isset($item[$key]['remove']) && $item[$key]['remove'] == 1) {
                                SetState('UserOrderId', null);
                                Yii::app()->shoppingCart->remove($position->id);
                            } else {
                                if ((int) $position->getQuantity() != (int) $item[$key]['qty']) {
                                    SetState('UserOrderId', null);
                                    $object = Object::model()->findByPk($position->id);
                                    Yii::app()->shoppingCart->update($object, $item[$key]['qty']);
                                }
                            }
                        }
                    }
                    if (Yii::app()->shoppingCart->getItemsCount() == 0) {
                        Yii::app()->request->redirect(Yii::app()->createUrl('page/render', array('slug' => 'card')));
                    } else {
                        Yii::app()->request->redirect(Yii::app()->createUrl('page/render', array('slug' => 'checkout')));
                    }
                }
            }
            $ObjectCard = Object::model()->GetObjectCart();
            $subtotal = Yii::app()->shoppingCart->getCost();           

            $this->render(BlockRenderWidget::setRenderOutput($this), array('ObjectCard' => $ObjectCard, 'subtotal' => $subtotal));
        } else {
            echo '';
        }
    }

    public function validate() {
        return true;
    }

    public function params() {
        return array();
    }

    public function beforeBlockSave() {
        return true;
    }

    public function afterBlockSave() {
        return true;
    }

}

?>